package com.liangfeng.readnews.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.liangfeng.readnews.R;
import com.liangfeng.readnews.bean.NewsBean;
import com.liangfeng.readnews.util.DisplayUtil;
import com.liangfeng.readnews.util.LogUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by mzf on 2017/4/21.
 */

public class MyAdapter extends BaseAdapter {

    List<NewsBean.Result.NewData> datas;
    Context context;

    public MyAdapter(List datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LogUtils.e("getView");
        ViewHolder viewHolder;
        //判断是否存在缓存
        if (convertView == null) {//不存在
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
            viewHolder.iv_pic_item = (ImageView) convertView.findViewById(R.id.iv_pic_item);
            viewHolder.tv_src_item = (TextView) convertView.findViewById(R.id.tv_src_item);
            viewHolder.tv_time_item = (TextView) convertView.findViewById(R.id.tv_time_item);
            viewHolder.tv_title_item = (TextView) convertView.findViewById(R.id.tv_title_item);
            viewHolder.rl_item = (RelativeLayout) convertView.findViewById(R.id.rl_item);

            //将viewHolder存入缓存中
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //向控件中填充数据
        NewsBean.Result.NewData newData = datas.get(position);

        viewHolder.tv_title_item.setText(newData.getTitle());
        viewHolder.tv_src_item.setText(newData.getSrc());

        //对时间进行处理
        String time = newData.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        int betweenTime = 1;
        try {
//            Date date = sdf.parse(time);
//            Date now = new Date();//当前时间
            long date = sdf.parse(time).getTime();
            long now = new Date().getTime();//当前时间
            betweenTime = (int) ((now - date) / (1000 * 60 * 60));//计算时间间隔
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY); //23，当前日
        viewHolder.tv_time_item.setText(betweenTime+"小时前");


        String pic = newData.getPic();
        if (!TextUtils.isEmpty(pic)) {//有图片
            Glide.with(context).load(pic).into(viewHolder.iv_pic_item);
        } else {
            viewHolder.iv_pic_item.setVisibility(View.GONE);
            ViewGroup.LayoutParams layoutParams = viewHolder.rl_item.getLayoutParams();
            layoutParams.width = DisplayUtil.dip2px(context, 320);
            viewHolder.rl_item.setLayoutParams(layoutParams);
        }

        return convertView;
    }

    class ViewHolder {
        TextView tv_title_item;
        TextView tv_src_item;
        TextView tv_time_item;
        ImageView iv_pic_item;
        RelativeLayout rl_item;
    }
}
